//
//  B_UserMainPageVC.m
//  DreamOf
//
//  Created by 薛小刚 on 2018/9/1.
//  Copyright © 2018年 薛小刚. All rights reserved.
//

#import "B_UserMainPageVC.h"
#import "B_UserBasicModel.h"
#import "B_ListCenterAlertView.h"
#import "B_MineHeaderView.h"
#import "B_MineHeaderMoreView.h"
#import "B_MyTopicListVC.h"
#import "B_MyActivityListVC.h"
#import "B_MineFooterView.h"
#import "B_MineTopicTVC.h"
#import "B_MineActivityTVC.h"
#import "B_LeaveMessagesVC.h"
#import "B_ReportListVC.h"
#import "B_ShareUserMainPageVC.h"
#import "B_LeaveListVC.h"
#import "B_BusinessShopVC.h"


@interface B_UserMainPageVC ()

@property (nonatomic, strong) BaseTableView * mainView;
/// 用户的基本信息的模型
@property (nonatomic, strong) B_UserBasicModel * userModel;

@property (nonatomic, strong) NSMutableArray * dataSource;

@end

@implementation B_UserMainPageVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view = self.mainView;
    WeakSelf(weSelf, self);

    [self setRightImageBarButtonItemWithFrame:CGRectZero image:@"B_topic_details_more" selectImage:@"" ishidden:YES action:^(GUIButton *button) {
        /// TODO:执行右侧点击事件
        if (weSelf.dataSource) {
            B_ListCenterAlertView * view = [B_ListCenterAlertView initB_ListCenterAlertView];
            [view show];
            view.dataArray = @[@"分享个人名片", @"举报", @"留言"];
            view.selectIndex = ^(NSIndexPath *indexPath) {
                if (indexPath.row == 0) {
                    B_ShareUserMainPageVC * share = [[B_ShareUserMainPageVC alloc] init];
                    share.B_userId = weSelf.B_userId;
                    [weSelf pushNextViewController:share];
                } else if (indexPath.row == 1) {
                    B_ReportListVC * report = [[B_ReportListVC alloc] init];
                    report.B_userID = weSelf.B_userId;
                    report.type = B_ReportListVCTypeUser;
                    [weSelf pushNextViewController:report];
                } else if (indexPath.row == 2) {
                    B_LeaveListVC * message = [[B_LeaveListVC alloc] init];
                    message.B_userId = getUserId();
                    message.type = B_LeaveListVCTypeFromMessage;
                    [weSelf pushNextViewController:message];
                }
            };
        }
    }];
    // 数据请求
    [self getData];
}


- (BaseTableView *)mainView {
    if (!_mainView) {
        WeakSelf(weSelf, self);
        _mainView = [[BaseTableView alloc] init];
        [_mainView setTableViewFrameWithTop:-getNavHeight() left:0 right:0 bottom:0];
        [_mainView.tableView registerNib:[UINib nibWithNibName:@"B_MineTopicTVC" bundle:nil] forCellReuseIdentifier:@"B_MineTopicTVC"];
        [_mainView.tableView registerNib:[UINib nibWithNibName:@"B_MineActivityTVC" bundle:nil] forCellReuseIdentifier:@"B_MineActivityTVC"];
        /// 组数
        _mainView.numberOfSectionsBlock = ^NSInteger(UITableView *tableView) {
            return 3;
        };
        /// cell数
        _mainView.numberOfRowsBlock = ^NSInteger(UITableView *tableView, NSInteger section) {
            if (section == 0) {
                return 0;
            } else {
                return 1;
            }
        };
        /// 组头的高度
        _mainView.heightForHeaderBlock = ^CGFloat(UITableView *tableView, NSInteger section) {
            if (section == 0) {
                /// 如果是商家  返回  384;   不是返回  340;
                return 384;
            } else {
                return 50;
            }
        };
        /// 组头
        _mainView.viewForHeaderBlock = ^UIView *(UITableView *tableView, NSInteger section) {
            if (section == 0) {
                B_MineHeaderView * header0 = [B_MineHeaderView initB_MineHeaderView];
                /// 是商家展示    不是  隐藏
                header0.B_merchantView.hidden = NO;
                
                
                
                header0.B_signView.hidden = YES;
                header0.B_footerPrintButton.hidden = YES;
                
                //                if (未关注) {
//
                header0.B_addAttention.hidden = NO;
                header0.B_integralView.hidden = YES;
                header0.B_collectionView.hidden = YES;
                
                //            } else {
                
//                header0.B_addAttention.hidden = YES;
//                header0.B_integralView.hidden = NO;
//                header0.B_collectionView.hidden = NO;
//                header0.B_collectonLabel.text = @"留言";
//                header0.B_integralLabel.hidden = YES;
//                header0.B_userIconImageView.hidden = NO;
//            }
                
                
                header0.B_userName.text = safeValue0(weSelf.userModel.nickName);
                header0.B_userSignature.text = safeValue0(weSelf.userModel.signature);
                NSString * sex = @"", * city = @"城市未填写";
                if ([weSelf.userModel.sex isEqualToString:@"1"]) {
                    sex = @"男";
                } else if ([weSelf.userModel.sex isEqualToString:@"2"]) {
                    sex = @"女";
                } else {
                    sex = @"性别未填写";
                }
                header0.B_sexLabel.text = sex;
                header0.B_ageLabel.text = [NSString stringWithFormat:@"%@ 岁", safeValue1(weSelf.userModel.age)];
                if (ISNULL(weSelf.userModel.city)) {
                    
                } else {
                    city = [NSString stringWithFormat:@"%@ %@ %@", weSelf.userModel.province, weSelf.userModel.city, weSelf.userModel.district];
                }
                header0.B_cityLabel.text = city;
                [header0.B_attentionButton setTitle:[NSString stringWithFormat:@"%@ 关注", safeValue1(weSelf.userModel.followCount)] forState:UIControlStateNormal];
                [header0.B_fansButton setTitle:[NSString stringWithFormat:@"%@ 粉丝", safeValue1(weSelf.userModel.fansCount)] forState:UIControlStateNormal];
                [header0.B_footerPrintButton setTitle:[NSString stringWithFormat:@"%@ 足迹", safeValue1(weSelf.userModel.trackCount)] forState:UIControlStateNormal];
                [header0.B_userImageView sd_setImageWithURL:[NSURL URLWithString:weSelf.userModel.portrait]];
                if ([weSelf.userModel.userType isEqualToString:@"2"]) {
                    header0.B_userStatusImageView.hidden = NO;
                    header0.B_userStatusImageView.image = [UIImage imageNamed:@"B_personal_center_real_name_user_icon"];
                } else if ([weSelf.userModel.userType isEqualToString:@"3"]) {
                    header0.B_userStatusImageView.hidden = NO;
                    header0.B_userStatusImageView.image = [UIImage imageNamed:@"B_personal_center_merchants_icon"];
                } else {
                    header0.B_userStatusImageView.hidden = YES;
                }
                header0.mineHeaderAction = ^(NSString *string) {
                    HXLog(@"string is -> %@", string);
                    if ([string isEqualToString:@"tx"]) {
                        //TODO:查看大图
//                    } else if ([string isEqualToString:@"qd"]) {
                        
                    } else if ([string isEqualToString:@"jf"]) {
                        //TODO:积分
                       
                    } else if ([string isEqualToString:@"sc"]) {
                        //TODO:留言
                        B_LeaveListVC * message = [[B_LeaveListVC alloc] init];
                        message.B_userId = getUserId();
                        message.type = B_LeaveListVCTypeFromMessage;
                        [weSelf pushNextViewController:message];
                    } else if ([string isEqualToString:@"jgz"]) {
                        //TODO:加关注
                        
                    } else if ([string isEqualToString:@"sj"]) {
                        //TODO:查看商家
                        B_BusinessShopVC * vc = [[B_BusinessShopVC alloc] init];
                        
                        [weSelf pushNextViewController:vc];
                    }
                };
                return header0;
            } else {
                B_MineHeaderMoreView * header1 = [B_MineHeaderMoreView initB_MineHeaderMoreView];
                NSArray * arr = @[@"我的话题", @"我的活动"];
                header1.B_titleLabel.text = arr[section - 1];
                header1.moreAction = ^{     //TODO:执行点击事件
                    if (section == 1) {
                        B_MyTopicListVC * myTopic = [[B_MyTopicListVC alloc] init];
                        [weSelf pushNextViewController:myTopic];
                    } else if (section == 2) {
                        B_MyActivityListVC * myActivity = [[B_MyActivityListVC alloc] init];
                        [weSelf pushNextViewController:myActivity];
                    }
                };
                return header1;
            }
            return nil;
        };
        /// 组尾的高度
        _mainView.heightForFooterBlock = ^CGFloat(UITableView *tableView, NSInteger section) {
            if (section == 0) {
                CGFloat height = (SCREEN_WIDTH - 4) /4.;
                if (weSelf.userModel.userPictureList.count % 4 == 0) {
                    return height * (weSelf.userModel.imageArray.count / 4);
                } else {
                    return height * (weSelf.userModel.imageArray.count / 4 + 1);
                }
                
            } else {
                return 0.1;
            }
        };
        /// 组尾
        _mainView.viewForFooterBlock = ^UIView *(UITableView *tableView, NSInteger section) {
            if (section == 0) {
                B_MineFooterView * footer = [B_MineFooterView initB_MineFooterView];
                footer.imageArray = weSelf.userModel.imageArray;
                footer.collectionTapAction = ^(NSIndexPath *indexPath) {
                    //TODO:点击看大图的
                    HXLog(@"点击看大图");
                };
                return footer;
            } else {
                return nil;
            }
            return nil;
        };
        /// cell
        _mainView.cellForRowBlock = ^UITableViewCell *(UITableView *tableView, NSIndexPath *indexPath) {
            if (indexPath.section == 0) {
                
            } else if (indexPath.section == 1) {
                B_MineTopicTVC * cell = [tableView dequeueReusableCellWithIdentifier:@"B_MineTopicTVC" forIndexPath:indexPath];
                
                return cell;
            } else if (indexPath.section == 2) {
                B_MineActivityTVC * cell = [tableView dequeueReusableCellWithIdentifier:@"B_MineActivityTVC" forIndexPath:indexPath];
                
                return cell;
            }
            UITableViewCell * cell = [[UITableViewCell alloc] init];
            return cell;
        };
        /// cell的高度
        _mainView.heightForRowBlock = ^CGFloat(UITableView *tableView, NSIndexPath *indexPath) {
            
            return 270;
        };
        
    }
    return _mainView;
}





- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self setNavigationBarBackgroundWithImage:[UIImage getImageWithColor:[UIColor clearColor] alp:0]];
    [self setNavigationBarShadowImge:[UIImage getImageWithColor:[UIColor clearColor] alp:0]];
    
}

- (void) viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self setNavigationBarShadowImge:[UIImage getImageWithColor:[UIColor color_main_background_color] alp:1]];
}
- (void)pushNextViewController:(UIViewController *)controller {
    [super pushNextViewController:controller];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)getData {
    WeakSelf(weSelf, self);
    [NetworkingManager getNetworkRequestWithUrlString:[NSString stringWithFormat:@"/yearnfor/inter/user/selectUserInfo/%@", self.B_userId] parameters:nil isCache:YES succeed:^(id data) {
        if ([data[@"code"] isEqualToString:@"20010"]) {
            weSelf.userModel = [B_UserBasicModel mj_objectWithKeyValues:data[@"values"]];
            weSelf.userModel.imageArray = [NSMutableArray array];
            for (NSDictionary * dic in weSelf.userModel.userPictureList) {
                [weSelf.userModel.imageArray addObject:dic[@"picture"]];
            }
            [weSelf.mainView.tableView reloadData];
            [weSelf.dataSource removeAllObjects];
            weSelf.dataSource = nil;
            [weSelf.dataArray removeAllObjects];
        } else {
            [KeyWindow makeToast:data[@"message"]];
        }
    } fail:^(NSError *error) {
        [KeyWindow makeToast:@"服务器异常"];
    }];
}


- (NSMutableArray *)dataSource {
    if (!_dataSource) {
        B_CellModel * model0 = [B_CellModel initB_CellModelWithTitle:@"分享个人名片" withControllers:NSStringFromClass(B_ShareUserMainPageVC.class)];
        B_CellModel * model1 = [B_CellModel initB_CellModelWithTitle:@"举报" withControllers:NSStringFromClass(B_ReportListVC.class)];
        B_CellModel * model2 = [B_CellModel initB_CellModelWithTitle:@"留言" withControllers:NSStringFromClass(B_LeaveMessagesVC.class)];
        
        _dataSource = [NSMutableArray arrayWithObjects:model0, model1, model2, nil];
    }
    return _dataSource;
}


/**
 添加关注
 */
- (void) addAttention {
    WeakSelf(weSelf, self);
    [NetworkingManager getNetworkRequestWithUrlString:[NSString stringWithFormat:@"/yearnfor/inter/userFollow/addDelUserFollow/%@/%@", getUserId(), self.B_userId] parameters:nil isCache:NO succeed:^(id data) {
        if ([data[@"code"] isEqualToString:@"20005"]) {
            [weSelf getData];
        } else {
           [KeyWindow makeToast:data[@"message"]];
        }
    } fail:^(NSError *error) {
        [KeyWindow makeToast:@"服务器异常"];
    }];
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
